package com.tianya.common.mail;

import java.util.Map;

import com.tianya.common.util.Utility;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.servlet.view.freemarker.*;   
import freemarker.template.*; 
public class TemplateEmailService {
 
	private JavaMailSender mailSender; 
	private String defaultuser;
	private FreeMarkerConfigurer freeMarkerConfigurer=null;//FreeMarker的技术类   
	      
	public void setFreeMarkerConfigurer(FreeMarkerConfigurer freeMarkerConfigurer) {   
	        this.freeMarkerConfigurer = freeMarkerConfigurer;   
	}     
	  
	//通过模板构造邮件内容
	@SuppressWarnings("unchecked")
	public String getMailText(String templateName,Map map){   
		String htmlText="";   
		try {   
			//通过指定模板名获取FreeMarker模板实例   
			Template tpl=freeMarkerConfigurer.getConfiguration().getTemplate(templateName);   
			tpl.setEncoding("GBK");
			//FreeMarker通过Map传递动态数据 ,解析模板并替换动态数据，   
			htmlText=FreeMarkerTemplateUtils.processTemplateIntoString(tpl,map);   
		}
		catch (Exception e) {   
			e.printStackTrace();   
		}   
		return htmlText;   
	} 
	public static TemplateEmailService getTemplateEmailService() 
	{     
		return (TemplateEmailService)Utility.getBean("templateEmailService");
	}


	public JavaMailSender getMailSender() {
		return mailSender;
	}


	public void setMailSender(JavaMailSender mailSender) {
		this.mailSender = mailSender;
	}


	public String getDefaultuser() {
		return defaultuser;
	}


	public void setDefaultuser(String defaultuser) {
		this.defaultuser = defaultuser;
	}

 
}
